/*****************************************************************************************/
/* ECSE 426 - Microprocessor - Fall 2009
/* Lab Project - Includes control for PS/2 (Keyboard)
/* testKEYBOARDmain.C
/* @author: Danny Wu - 260238209 - B.Comp.Eng
/* @author: Han-Sheng Chin - 260209715 - B.Elec.Eng
/* @author: Benoit Gravel - 260230976 - B.Comp.Eng
/* @author: Bradley Thompson - 260224857 - B.Comp.Eng

/* @version: 12.11.15.48
/****************************************************************************************/
//#include <msp430x14x.h>
//#include <constants.h>
//#include <uart.h>
//
////other main vars
//
//int state;    // Global variable for state of the finite state machine.
//int func;     // Global variable for determining which option to use in input interrupt.
//int exit;     // Global variable for determining whether to exit program or not.
//int input;    // Global variable to store the input values from the keyboard.
//int waitInput;  // Global variable to determine if the FSM is waiting for input.
//int regAdd;     // Global variable for the register address to be modified.
//int regVal;     // Global variable for the new register value.
//char msg[64];     // message to be transmitted
//int msgCtr;     //counter for msg length
//
//int cc2500add[14] = {0x30, 0x31, 0x32, 0x33,0x34, 0x35, 0x36, 0x37,0x38, 0x39, 0x3A, 0x3B,0x3C,0x3D};
//char *cc2500name[14] = {"PARTNUM", "VERSION", "FREQEST", "LQI",
//"RSSI", "MARCSTATE", "WORTIME1", "WORTIME0", "PKSTATUS", "VCO_VC_DAC",
//"TXBYTES", "RXBYTES", "RCCTRL1_STATUS", "RCCTRL0_STATUS"};
//char *cc2500configReg[0x2F] = {"IOCFG2","IOCFG1","IOCFG0","FIFOTHR","SYNC1","SYNC0","PKTLEN","PKTCTRL1","PKTCTRL0","ADDR","CHANNR","FSCTRL1","FSCTRL0","FREQ2","FREQ1","FREQ0",
//"MDMCFG4","MDMCFG3","MDMCFG2","MDMCFG1","MDMCFG0","DEVIATN","MCSM2","MCSM1","MCSM0","FOCCFG","BSCFG","AGCTRL2","AGCTRL1","AGCTRL0","WOREVT1","WOREVT0",
//"WORCTRL","FREND1","FREND0","FSCAL3","FSCAL2","FSCAL1","FSCAL0","RCCTRL1","RCCTRL0","FSTEST","PTEST","AGCTEST","TEST2","TEST1","TEST0"};
//
////TI variables
//char txBuffer[4];
//char rxBuffer[4];
////end other main vars
//
//
////prototypes
//void initKeyboard (void);
////int HostReceive (void) __interrupt[PORT1_VECTOR];
//void ParseCode(void);
//int DataLine(void);
//int ClockLine(void);
//char lookupKey(int dataVal);
//void KeyboardReset (void);
//
////global variables
//int KeyboardState=0;
//int KeyboardReceiveBit=0;
//int KeyboardDataBuffer;
//int BreakSeq =0;
//char key;
//int count;
//char j=0;
//int k=0;
//char array[50];
//int arrayIndex=0;
//char dataarr[11];
//char dstart,d0,d1,d2,d3,d4,d5,d6,d7,dp,dstop;
//int seq;
////2.7 -in: flag when shiftreg full
////2.6 -in: error bit
////2.0 -out: reset, always 0
//
//void initKeyboard (void){
//  //init VHDL code part
//  P4SEL = 0x00; //I/O
//  P4DIR = 0x00; //input
//  P2SEL &= 0x3E;  //P2.7,2.6,2.0 I/O
//  P2DIR &= 0x7F;  //P2.7 input
//  P2DIR &= 0xBF;  //P2.6 input
//  P2DIR |= 0x01;  //P2.0 output
//  P2OUT &= 0xFE;  //p2.0 low
//  //P2IES = 0x10;
//  P2IES = 0x00;
//  P2IE = 0xC0;
//  //P2IE = 0x00;
//  P2IFG = 0x00;
//
//  //initialize keyboard (P1.4 -->CLOCK line, P1.5-->DATA line)
//  P1SEL &= 0xCF;                // I/O for P1.4,1.5
//  P1DIR &= 0xCF;                // input (P1.4,1.5)
//  P1IES = 0x10;                 //Clock interupt on high to low transitions
//  P1IE = 0x00;              //test disabled interupts
//  //P1IE = 0x10;                  //Enable interupts on clock line
//  P1IFG = 0x00;                 // Clear interrupt flag
//
//  //testing probe start
//  P1SEL &= 0x3F;    //I/O for P1.6,1.7
//  P1DIR |= 0xC0;    //output
//  P1OUT |= 0xC0;    //iddle output 1
//
//  //testing probe end
//  return ;
//}//end initKeyboard
//
//int HostReceive (void) __interrupt[PORT1_VECTOR]{
//
//  int s=0;
//
//  if((P1IN & 0x10)== 0x00){
////    for (s;s<1;s++){  //real clock edge?
////      if ((P1IN & 0x10)!= 0x00){
////        P1IFG = 0x00;
////        return 0;
////      }
////    }
//  
//    if (seq=0)
//     P1OUT &= 0xBF;    //P1.6 zero
//    else{
//      if (seq%2 != 0)
//        P1OUT |= 0x40;  //p1.6 high
//      else
//        P1OUT &= 0xBF;    //P1.6 zero
//      if (seq == 10)
//        seq=0;
//    }
//  ////  for(s;s<11;s++){
//  ////    if (s%2 != 0){
//  ////      
//  ////      P1OUT &= 0xBF;    //P1.6 zero
//  ////    }
//  ////    else
//  ////      P1OUT |= 0x40;  //p1.6 high
//  ////    wait(20);
//  ////  }
//  }
//
////if((P1IN & 0x10)== 0x00){
//
////  if ((P1IN & 0x10)== 0x00)
////    P1OUT |= 0x40;  //p1.6 high
////    //P1OUT ^= 0x40;  //p1.6 high
////  else
////     P1OUT &= 0xBF;    //P1.6 zero
//
////}
//////  if((P1IN & 0x20)== 0x00)
//////    dataarr[k]='0';
//////  else
//////    dataarr[k]='1';
//////
//////  if (k==10){
//////    println(dataarr);
//////    k=0;
//////  }
//////  else
//////    k++; 
//
//  P1IFG = 0x00;                 // Clear interrupt flag
//}//end HostReceive interrupt
//
//
//////int HostReceive (void) __interrupt[PORT1_VECTOR]{
//////  int i;
//////  int parity;
//////  int mask;
//////  char Data;
////// 
////// //// if (count>=2000){
////// ////   println("");
//////  // array[arrayIndex]='s'; 
//////  //  arrayIndex++;
////// //// }
//////  //println("Value at bit: ");
//////  //printHex(count);
//////  //print(" is ");
//////  //quick test
//////////  if(DataLine()==1)
//////////    putChar('1');
//////////  else
//////////    putChar('0');  
//////
//////  //j++;
//////  //arrayIndex++;
//////  if(j == 11){
//////    j = 0;
//////    //println("");
//////  }
//////
////////  if (arrayIndex >= 49){
////////    println("array:");
////////    arrayIndex=0;
////////    for(;arrayIndex<50;arrayIndex++){
////////      putChar(array[arrayIndex]);
////////    }
////////    arrayIndex=0;
////////  }
////////  count=0;
//////
//////if (count>=1000)
//////   println("s");
//////
//////
////////  if (j==1)
////////    print(" data=");
////////  if (j==9)
////////    print(" par=");
////////  if (j==10)
////////    print(" stop=");
////////  if (j==11)
////////    print(" ack=");
//////  if(DataLine()==1)
//////    putChar('1');
//////  else
//////    putChar('0');
//////  j++;
//////  if(j==12)//12)
//////    j=0;
//////  count=0;
//////  //end quick test
//////
//////
////////  switch(KeyboardState){
////////      
////////      case 0: //idle
////////        if ((DataLine()== 0) ){ //Start Bit
////////          KeyboardState = 2; //go into HostReceive mode
////////          KeyboardReceiveBit = 1; //Start Bit received
////////          KeyboardDataBuffer = 0;
////////        }
////////      break;
////////      
////////      case 1: //HostSend
////////        //should never reach here since interupts disabled in HOST SEND
////////      break;
////////      
////////      case 2: //HostReceive (DeviceSend)
////////        if (KeyboardReceiveBit<= 9){  //if Data bits
////////          //Data = DataLine()
////////          
////////         // KeyboardDataBuffer << 1;
////////          //KeyboardDataBuffer += DataLine();
////////          KeyboardDataBuffer += DataLine()*(2^((KeyboardReceiveBit -1)));
////////          KeyboardReceiveBit++;
////////        }
////////        else if (KeyboardReceiveBit == 10){ //parity bit
////////          parity=0;
////////          mask=0;
////////
////////          for(i=0;i<8;i++){
////////            mask = 2^i;
////////            if ((KeyboardDataBuffer & mask) != 0){
////////              parity++;
////////            }
////////          }
////////          parity = parity%2;
////////          if( DataLine() != !parity){//if expected parity error
////////            //TODO : hostSend(_Resend);
////////            break;
////////          }
////////          else{
////////            KeyboardReceiveBit++;
////////          }
////////        }
////////        else{ //Stop Bit
////////          if(DataLine()==1){ // if Stop bit on DATA line
////////            KeyboardReceiveBit = 0;
////////            KeyboardState = 0;  //go back into iddle state
////////            ParseCode();
////////          }
////////        }
////////      break;
////////  }//end switch  
//////  P1IFG = 0x00;                 // Clear interrupt flag
//////}//end HostReceive interrupt
//
////Parses content of KeyboardDataBuffer
//void ParseCode(void){
//   key = 0;// lookupKey();  
//  if (BreakSeq == '1'){//this key was released
//    BreakSeq = 0;    
//  }
//  else{//this key was pressed
//    if (KeyboardDataBuffer == 0xF0){//Break code
//      BreakSeq = 1; //signal BreakCode for next key
//    }
//    else if (key != 0){
//      key =key;//TODO print key;
//    }
//  }
//}
//
//
//
//
////Helper function returns DataLine input
//int DataLine(void){
//  if((P1IN & 0x20)== 0x00)
//    return 0;
//  else
//    return 1;
//}
////Helper function returns ClockLine input
//int ClockLine(void){
//  if((P1IN & 0x10)== 0x00)
//    return 0;
//  else
//    return 1;
//}
//
////Lookup table for Keyboard ScanCode --> ACSII char
//char lookupKey(int data){
//  switch(data){
//    case(0x0E): return '~';
//    case(0x16): return '1';
//    case(0x1E): return '2';
//    case(0x26): return '3';
//    case(0x25): return '4';
//    case(0x2E): return '5';
//    case(0x36): return '6';
//    case(0x3D): return '7';
//    case(0x3E): return '8';
//    case(0x46): return '9';
//    case(0x45): return '0';
//    case(0x4E): return '-';
//    case(0x15): return 'Q';
//    case(0x1D): return 'W';
//    case(0x24): return 'E';
//    case(0x2D): return 'R';
//    case(0x2C): return 'T';
//    case(0x35): return 'Y';
//    case(0x3C): return 'U';
//    case(0x43): return 'I';
//    case(0x44): return 'O';
//    case(0x4D): return 'P';
//    case(0x54): return '[';
//    case(0x5B): return ']';
//    case(0x1C): return 'A';
//    case(0x1B): return 'S';
//    case(0x23): return 'D';
//    case(0x2B): return 'F';
//    case(0x34): return 'G';
//    case(0x33): return 'H';
//    case(0x3B): return 'J';
//    case(0x42): return 'K';
//    case(0x4B): return 'L';
//    case(0x1A): return 'Z';
//    case(0x22): return 'X';
//    case(0x21): return 'C';
//    case(0x2A): return 'V';
//    case(0x32): return 'B';
//    case(0x31): return 'N';
//    case(0x3A): return 'M';
//    case(0x41): return ',';
//    case(0x49): return '.';
//    case(0x4A): return '/';
//    case(0x29): return ' ';//SPACE
//    //added
//    case(0x5A): return '\n'; //ENTER
//    //case(0x76): return 'ESC'; //ESCAPE
//    //case(0x66): return 'BS'; //Backspace
//    default: return  0;//NULL
//  }//end switch
//}
//
//
////Timming testing
//char printInt(int val){
//  if ( (val > 0 )&& (val < 2))
//    return 'a';
//  if( val<10)
//    return 'b';
//  if( val<100)
//    return 'c';
//  if( val<1000)
//    return 'd';
//  else
//    return 'e';
//}
//
////Print a 8bit hex bit by bit
//void printVal(int dataVal){
//  if((dataVal & 0x80) == 0x80)
//    putChar('1');
//  else
//    putChar('0');
//  if((dataVal&0x40)==0x40)
//    putChar('1');
//  else
//    putChar('0');
//  if((dataVal&0x20)==0x20)
//    putChar('1');
//  else
//    putChar('0');
//  if((dataVal&0x10)==0x10)
//    putChar('1');
//  else
//    putChar('0');
//  if((dataVal&0x08)==0x08)
//    putChar('1');
//  else
//    putChar('0');
//  if((dataVal&0x04)==0x04)
//    putChar('1');
//  else
//    putChar('0');
//  if((dataVal&0x02)==0x02)
//    putChar('1');
//  else
//    putChar('0');
//  if((dataVal&0x01)==0x01)
//    putChar('1');
//  else
//    putChar('0');
//  //println(" ");
//  putChar(' ');
//  putChar(',');
//  putChar(' ');
//}
////interrupt vector for data ready on cpld
//int KeyReceive (void) __interrupt[PORT2_VECTOR]{
//  int dataVal;
//  char Val;
//  if ((P2IN & 0x80) == 0x80){
//    if ((P2IN & 0x40) == 0x40){
//      println("error");
//      KeyboardReset();
//    }
//    else{
//      dataVal = P4IN;
//      printVal(dataVal);
//      Val = lookupKey(dataVal);
//      putChar(Val);
//      println(" ");
//    }
//  }
//  else if ((P2IN & 0x40) == 0x40){
//      println("error");
//  }
//  P2IFG = 0x00;                 // Clear interrupt flag
//}
////Reset the shifters in the cpld
//void KeyboardReset (void){
//  wait(1000);
//  P2OUT |= 0x01;  //reset shifter in cpld
//  wait(1000);
//  P2OUT &= 0xFE;  //set 2.0 low for not reset
//}
//
//void main(void){
//  char x;
//  int i=0;
//  char regNum;
//  int dataVal;
//
//  //initialize configuration of the clock
////    WDTCTL = WDTPW +WDTHOLD;                  // Stop Watchdog Timer
////  DCOCTL = DCO0 + DCO1 + DCO2;              // Max DCO
////  BCSCTL1 = RSEL0 + RSEL1 + RSEL2;          // XT2on, max RSEL
////  BCSCTL2 |= SELS;                          // SMCLK = XT2
//  
//  WDTCTL = WDTPW + WDTHOLD;             // Stop WDT
////BCSCTL1 = XTS;
////BCSCTL2 = 0xC0;
//  BCSCTL1 &= ~XT2OFF;                   // XT2OFF
//  BCSCTL2 |= SELS;                      // MCLK = SMCLK = XT2 (safe)
//  _EINT();                              // Enable interrupts
//  
//  
//  initRsUART();
//  //initSpiUART();
//  clearScreen();  
//  //ccPowerUp(); //includes a reset
//  //ccResetChip();  
//  //ccConfigBeacon();
//
//  count = 0;
//  P5SEL = 0x10; //MCLK output for testing on pin 5.4
//  initKeyboard();
//  KeyboardReset();
//
// // while(1);
//     println("start");
//////     while(1){//VHDL loop
//////        if ((P2IN & 0x40)== 0x40){//error
//////          putChar('E');
//////        }
//////     }
//
////  if((P2IN & 0x40)== 0x40)
////    regNum = 0x40;
////  else
////    regNum = 0x00;
//
//    while(1){
//    if ((P2IN & 0x80) == 0x80)
//      putChar('i');
//    else if ((P2IN & 0x40) == 0x40)
//      putChar('e');
//////        if((P2IN & 0x40) != regNum){
//////          dataVal = P4IN;
//////          printVal(dataVal);
//////          regNum = P2IN & 0x40;
//////        }
////      count++;
////      if (count>=30000){
////        count=0;
////        k=0;
////        //println("reset count");
////      }
//    }
////  while(1){
////    //probe test
////    if( ClockLine() == 0){//falling edge
////      P1OUT &= 0xBF;  //set 1.6 low
////      if( DataLine() == 1){
////        P1OUT |= 0x80; //set 1.7 high
////        //putChar('a');
////      }
////      else{
////        P1OUT &= 0x7F;  //set 1.7 low
////        //putChar('b');
////      }
////      //wait(100);
////      P1OUT |= 0xC0;    //set 1.6 & 1.7 high (iddle mode)
////    
////      putChar(printInt(count));
////      count =0;
////    }
////    //end probe test
////    count++;
////    if (count>=65000){
////      count=0;
////      //println("reset count");
////    }
////  }
//}


//#include <msp430x14x.h>
//#include <string.h>
//#include <keyboard.h>
#include <msp430x14x.h>
#include <constants.h>
#include <keyboard.h>
#include <uart.h>
int state;    // Global variable for state of the finite state machine.
int func;     // Global variable for determining which option to use in input interrupt.
int exit;     // Global variable for determining whether to exit program or not.
int input;    // Global variable to store the input values from the keyboard.
int waitInput;  // Global variable to determine if the FSM is waiting for input.
int regAdd;     // Global variable for the register address to be modified.
int regVal;     // Global variable for the new register value.
char msg[64];     // message to be transmitted
int msgCtr;     //counter for msg length

int cc2500add[14] = {0x30, 0x31, 0x32, 0x33,0x34, 0x35, 0x36, 0x37,0x38, 0x39, 0x3A, 0x3B,0x3C,0x3D};
char *cc2500name[14] = {"PARTNUM", "VERSION", "FREQEST", "LQI",
"RSSI", "MARCSTATE", "WORTIME1", "WORTIME0", "PKSTATUS", "VCO_VC_DAC",
"TXBYTES", "RXBYTES", "RCCTRL1_STATUS", "RCCTRL0_STATUS"};
char *cc2500configReg[0x2F] = {"IOCFG2","IOCFG1","IOCFG0","FIFOTHR","SYNC1","SYNC0","PKTLEN","PKTCTRL1","PKTCTRL0","ADDR","CHANNR","FSCTRL1","FSCTRL0","FREQ2","FREQ1","FREQ0",
"MDMCFG4","MDMCFG3","MDMCFG2","MDMCFG1","MDMCFG0","DEVIATN","MCSM2","MCSM1","MCSM0","FOCCFG","BSCFG","AGCTRL2","AGCTRL1","AGCTRL0","WOREVT1","WOREVT0",
"WORCTRL","FREND1","FREND0","FSCAL3","FSCAL2","FSCAL1","FSCAL0","RCCTRL1","RCCTRL0","FSTEST","PTEST","AGCTEST","TEST2","TEST1","TEST0"};

//int key_press_flag;       // Keypress
//int user_input;           // Input location entered by the user
//int keyboard_LUT[256] = {
//0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   9,  '`',  0,
//0,   0,   0,   0,   0,  'Q', '1',  0,   0,   0,  'Z', 'S', 'A', 'W', '2',  0,
//0,  'C', 'X', 'D', 'E', '4', '3',  0,   0,  32,  'V', 'F', 'T', 'R', '5',  0,
//0,  'N', 'B', 'H', 'G', 'Y', '6',  0,   0,   0,  'M', 'J', 'U', '7', '8',  0,
//0,  ',', 'K', 'I', 'O', '0', '9',  0,   0,  '.', '/', 'L', ';', 'P', '-',  0,
//0,   0,  39,   0,  '[', '=',  0,   0,   0,   0,   10, ']',  0,   92,  0,   0,
//0,   0,   0,   0,   0,   0,   8,   0,   0,  '1',  0,  '4', '7',  0,   0,   0,
//'0','.', '2', '5', '6', '8',  27,  0,   0,  '+', '3', '-', '*', '9',  0,   0,
//0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
//0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
//0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
//0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
//0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
//0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
//0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
//0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
// };
void main(void){
  //initialize configuration of the clock
//    WDTCTL = WDTPW +WDTHOLD;                  // Stop Watchdog Timer
//  DCOCTL = DCO0 + DCO1 + DCO2;              // Max DCO
//  BCSCTL1 = RSEL0 + RSEL1 + RSEL2;          // XT2on, max RSEL
//  BCSCTL2 |= SELS;                          // SMCLK = XT2
  
  WDTCTL = WDTPW + WDTHOLD;             // Stop WDT
//BCSCTL1 = XTS;
//BCSCTL2 = 0xC0;
  BCSCTL1 &= ~XT2OFF;                   // XT2OFF
  BCSCTL2 |= SELS;                      // MCLK = SMCLK = XT2 (safe)
  _EINT();                              // Enable interrupts
  
  
  initRsUART();
  //initSpiUART();
  clearScreen();
  keyboardInit();
////initialize keyboard (P1.4 -->CLOCK line, P1.5-->DATA line)
// P1SEL &= 0xCF;                // I/O for P1.4,1.5
//  P1DIR &= 0xCF;                // input (P1.4,1.5)
//  P1IES = 0x10;                 //Clock interupt on high to low transitions
//  //P1IE = 0x00;              //test disabled interupts
//  P1IE = 0x10;                  //Enable interupts on clock line
//  P1IFG = 0x00;                 // Clear interrupt flag

  while (1){

  }
}
////his-->mine
////clock 1.2(0x04)-->1.4(0x10)
////data  1.1(0x02)-->1.5(0x20)
//void PORT1_isr(void) __interrupt[PORT1_VECTOR]
//{
// char key;
// key_press_flag = 1;
// P1IE = 0x00;              // Disable interrupts
// key=get_trace();
//
// P1IFG = 0x00;             // Reset interrupt flag
// P1IE = 0x10; //0x04;              // Enable interrupts
// putChar(key);
//}
//
//
//char get_trace()
//{
//  static int clk[50];
//  static int arr[50];
//  int i,j;
//  char last;
//  char key=0;
//  for (i=0;i<50;i++)
//  {
//   clk[i]=P1IN&0x10; //0x04;
//   arr[i]=P1IN&0x20; //0x02; 
//  }
// last=1;
// j=0;
// for (i=0;i<50;i++)
// {
//   if (last&&(clk[i]==0))
//   {
//     if (arr[i]&0x20)//0x02)
//     {
//       key |= 1<<j;
//     }
//     j++;
//   }
//   last=!(clk[i]==0);
// }
// key=keyboard_LUT[key];
//
// return(key);
//}
